package com.itheima.app.manager;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.itheima.app.interceptor.UserHolder;
import com.itheima.autoconfig.huanxin.HuanXinTemplate;
import com.itheima.domain.db.UserInfo;
import com.itheima.domain.vo.PageBeanVo;
import com.itheima.domain.vo.UserLikeCountVo;
import com.itheima.domain.vo.UserLikeVo;
import com.itheima.service.db.UserInfoService;
import com.itheima.service.mongo.FriendService;
import com.itheima.service.mongo.UserLikeService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserLikeManager {

    @Reference
    private UserLikeService userLikeService;

    @Reference
    private FriendService friendService;

    @Autowired
    private HuanXinTemplate huanXinTemplate;

    @Reference
    private UserInfoService userInfoService;

    // 探花-喜欢
    public void tanhuaLove(Long likeUserId) {
        // 1.获取登录用户id
        Long userId = UserHolder.get().getId();

        // 2.调用rpc完成喜欢功能
        userLikeService.save(userId, likeUserId);

        // 3.查询双方是否为相互喜欢
        Boolean isMutual = userLikeService.isMutualLike(userId, likeUserId);

        // 4.如果是
        if (isMutual) {
            // mongo添加好友
            friendService.addContact(userId, likeUserId);
            // 环信添加好友（二个用户必须在环信已经注册了查询）
            huanXinTemplate.addContacts(userId, likeUserId);
        }
    }

    public void tanhuaUnLove(Long likeUserId) {
        // 1.获取登录用户id
        Long userId = UserHolder.get().getId();
        // 2.判断双方是否为相互喜欢
        Boolean isMutual = userLikeService.isMutualLike(userId, likeUserId);
        // 3.如果是
        if (isMutual) {
            // 删除mongo好友
            friendService.deleteContact(userId, likeUserId);
            // 删除环信好友
            huanXinTemplate.deleteContacts(userId, likeUserId);
        }
        // 4.取消喜欢对方
        userLikeService.delete(userId, likeUserId);
    }

    public ResponseEntity userCount() {
        // 1.获取登录用户id
        Long userId = UserHolder.get().getId();
        // 2.调用rpc查询
        UserLikeCountVo userLikeCountVo = userLikeService.findUserLikeCount(userId);
        // 3.返回结果
        return ResponseEntity.ok(userLikeCountVo);
    }

    public ResponseEntity findUserDetailByPage(Integer type, Integer pageNum, Integer pageSize) {
        // 1.获取登录用户id
        Long userId = UserHolder.get().getId();
        // 2.调用rpc查询对方用户id分页对象
        PageBeanVo pageBeanVo = userLikeService.findByPage(userId, type, pageNum, pageSize);

        // 3.完善数据
        List<UserLikeVo> voList = (List<UserLikeVo>) pageBeanVo.getItems();
        if (CollectionUtil.isNotEmpty(voList)) {
            for (UserLikeVo userLikeVo : voList) {
                // 获取对方id
                Long friendId = userLikeVo.getId();
                // 查询对方用户详情
                UserInfo userInfo = userInfoService.findById(friendId);
                BeanUtil.copyProperties(userInfo, userLikeVo);
            }
        }

        // 4.返回结果
        return ResponseEntity.ok(pageBeanVo);
    }
}
